package com.hjs.mr.merge_and_sort;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * 这个是自定义的RecordReader，用于定义数据读取之后如何进行初步的处理，泛型的类型和InputFormat一致
 */
public class MergeRecordReader extends RecordReader<Text, BytesWritable> {

    //定义在整合过程之中需要使用到的成员变量
    private FileSplit split;
    private Configuration configuration;
    private Text key = new Text();
    private BytesWritable value = new BytesWritable();
    private Boolean flag = true;

    @Override
    public void initialize(InputSplit inputSplit, TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
        split = (FileSplit) inputSplit;
        configuration = taskAttemptContext.getConfiguration();
    }

    @Override
    public boolean nextKeyValue() throws IOException, InterruptedException {
        //针对每一个传入的value进行处理，并将处理的结果写入成员变量，以便之后的其他方法进行调用
        if (flag) {
            byte[] content = new byte[(int) split.getLength()];
            Path path = split.getPath();
            FileSystem fileSystem = path.getFileSystem(configuration);
            FSDataInputStream inputStream = fileSystem.open(path);
            IOUtils.readFully(inputStream, content, 0, content.length);
            key.set(path.toString());//这个是读取文件的路径
            value.set(content, 0, content.length);//这个是读取到的文件内容
            IOUtils.closeStream(inputStream);
            flag = false;
            return true;
        }
        return false;
    }

    /**
     * 返回当前读取到的数据的key
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public Text getCurrentKey() throws IOException, InterruptedException {
        return key;
    }

    /**
     * 返回当前读取到的数据的value
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public BytesWritable getCurrentValue() throws IOException, InterruptedException {
        return value;
    }

    @Override
    public float getProgress() throws IOException, InterruptedException {
        return 0;
    }

    @Override
    public void close() throws IOException {

    }
}
